package com.bases.ScollBar
{
	import com.Timer.FrameTimerEvent;
	import com.debug.TDebug;
	
	import config.TConfig;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * 普通控件的ScrollBar(非TextField)
	 * @author pethan
	 * 
	 */	
	public class TControlScrollBar extends TBaseScrollBar
	{
		private var controlTarget:Sprite;
		public var maskHeight:Number;
		private var thisWidth:Number;
		private var yMax:Number = 0;
		private var speed:Number = 10;
		private var yMin:Number = 0;
		private var targetHeight:Number=0;
		public function TControlScrollBar(_target:Sprite)
		{
			super();
			this.controlTarget=_target;
			this.yMin=TBaseScrollBar.BUTTON_HEIGHT;
		}
		
		public function invalidateScrollBar():void{
			targetHeight=int(controlTarget.height);
			//trace("罩遮高度:" + targetHeight)
			var isScroll:Boolean = targetHeight > maskHeight; //区域大罩遮高
			if (isScroll)
			{
				this.thisWidth = (this.btnTrack.height - TBaseScrollBar.BUTTON_HEIGHT * 2) * (1 - (targetHeight - maskHeight) / targetHeight);
//				this.btnScroll.setSize(this.btnScroll.width,this.thisWidth);  //滑块高
				this.btnScroll.y = this.btnUp.y + TBaseScrollBar.BUTTON_HEIGHT;
			}
			this.yMax=this.btnDown.y-this.btnScroll.height;
			thumbScrollV();//改变位置
			
			if (isScroll) {
				this.visible=true;
				//trace("舞台:" + TConfig.getInstance().mainStage)
				TConfig.getInstance().mainStage.addEventListener(MouseEvent.MOUSE_WHEEL,onWheel);
			} else {
				this.visible=false;
				TConfig.getInstance().mainStage.removeEventListener(MouseEvent.MOUSE_WHEEL,onWheel);
			}
		}
		
		public function invalidateScrollBarHeight(_height:Number):void{
			var isScroll:Boolean =_height > maskHeight;
			targetHeight=_height;
			if (isScroll) {
				this.btnTrack.setSize(this.btnTrack.width,maskHeight-2*TBaseScrollBar.BUTTON_HEIGHT);
				this.thisWidth = (this.btnTrack.height) * (1 - (_height - maskHeight) / _height);
				this.btnScroll.setSize(this.btnScroll.width,this.thisWidth);
				this.btnScroll.y = this.btnUp.y + TBaseScrollBar.BUTTON_HEIGHT;
				this.btnDown.y=this.btnScroll.y+this.btnTrack.height;
			}
			this.yMax=this.btnDown.y-this.btnScroll.height;
			if (isScroll) {
				this.visible=true;
				thumbScrollV();//改变位置
			}
			else {
				this.controlTarget.y=0;
				this.visible=false;
			}
		}
		
		public function invalidate():void{
			var scrollPercent:Number=-this.controlTarget.y/(targetHeight - maskHeight);
			btnScroll.y=scrollPercent*(yMax - TBaseScrollBar.BUTTON_HEIGHT)+TBaseScrollBar.BUTTON_HEIGHT;
		}
		
		override protected function onBackBGDown(event:MouseEvent):void{
			super.onBackBGDown(event);
			thumbScrollV();
		}
		
		
		override protected function onUp(event:MouseEvent):void{
			this.btnScroll.y -= this.speed;
			if (btnScroll.y <= yMin)
			{
				btnScroll.y = yMin;
			}
			super.onUp(event);
		}
		
		override protected function onDown(event:MouseEvent):void{
			this.btnScroll.y += this.speed;
			if (btnScroll.y >= yMax)
			{
				btnScroll.y = yMax;
			}
			super.onDown(event);
		}
		
		override protected function onTimer(event:FrameTimerEvent):void{
			if(type=='down'){
				btnScroll.y+= this.speed;
				if(this.btnScroll.y>=yMax){
					btnScroll.y=yMax;
				}
			}else{
				btnScroll.y-= this.speed;
				if(this.btnScroll.y<=this.yMin){
					this.btnScroll.y=this.yMin;
				}
			}
			super.onTimer(event);
		}
		
		override protected function thumbScrollV():void{
			var scrollPercent:Number=(btnScroll.y - TBaseScrollBar.BUTTON_HEIGHT) / (yMax - TBaseScrollBar.BUTTON_HEIGHT);
			this.controlTarget.y = - scrollPercent * (targetHeight - maskHeight);
		}
		
		/**
		 * 反向求出滚动的位置 
		 * 
		 */		
		public function changeScrollV():void{
			if(targetHeight>maskHeight){
				this.visible=true;
			}else{
				return;
			}
			var scrollPercent:Number=-this.controlTarget.y/ (targetHeight - maskHeight);
			btnScroll.y=scrollPercent* (yMax );
		}
		
		override protected function onStateMove(event:MouseEvent):void{
			thumbScrollV();
		}
		
		override protected function onWheel(event:MouseEvent):void{
			this.btnScroll.y -= event.delta*this.speed;
			if (btnScroll.y <= yMin)
			{
				btnScroll.y = yMin;
			}
			if (btnScroll.y >= yMax)
			{
				btnScroll.y = yMax;
			}
			super.onWheel(event);
		}
		
		override public function dispose():void{
			super.dispose();
			this.controlTarget=null;
		}
	}
}